//You are given an integer array coins representing coins of different denominat
//ions and an integer amount representing a total amount of money. 
//
// Return the fewest number of coins that you need to make up that amount. If th
//at amount of money cannot be made up by any combination of the coins, return -1.
// 
//
// You may assume that you have an infinite number of each kind of coin. 
//
// 
// Example 1: 
//
// 
//Input: coins = [1,2,5], amount = 11
//Output: 3
//Explanation: 11 = 5 + 5 + 1
// 
//
// Example 2: 
//
// 
//Input: coins = [2], amount = 3
//Output: -1
// 
//
// Example 3: 
//
// 
//Input: coins = [1], amount = 0
//Output: 0
// 
//
// 
// Constraints: 
//
// 
// 1 <= coins.length <= 12 
// 1 <= coins[i] <= 231 - 1 
// 0 <= amount <= 104 
// 
// Related Topics 广度优先搜索 数组 动态规划 
// 👍 1671 👎 0


package leetcode.editor.cn;

import java.util.Arrays;

//Java：Coin Change
public class P322CoinChange {
    public static void main(String[] args) {
        Solution solution = new P322CoinChange().new Solution();
        // TO TEST
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        int[] memo;

        public int coinChangeSta(int[] nums, int amount) {
            memo = new int[amount + 1];
            int change = change(nums, amount);
            return change;
        }

        public int change(int[] nums, int amount) {
            if (amount == 0) {
                return 0;
            }
            if (memo[amount] != 0) {
                return memo[amount];
            }
            int min = Integer.MAX_VALUE;
            for (int num : nums) {
                if (amount - num < 0) {
                    continue;
                }
                int res = change(nums, amount - num);
                if (res >= 0 && res < min) {
                    min = res + 1;
                }
            }
            memo[amount] = (min == Integer.MAX_VALUE) ? -1 : min;
            return memo[amount];
        }

        public int coinChange(int[] nums, int amount) {
            int[] arr = new int[amount + 1];
            Arrays.fill(arr, amount + 1);
            arr[0] = 0;
            for (int i = 1; i <= amount; i++) {
                for (int num : nums) {
                    if (i >= num) {
                        arr[i] = Math.min(arr[i], 1 + arr[i - num]);
                    }
                }
            }
            return arr[amount] > amount ? -1 : arr[amount];
        }
    }//leetcode submit region end(Prohibit modification and deletion)

}